package com.mobilekites.ad;

import android.app.Activity;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.mobilekites.MobileKitesApp;

public interface Admob extends CircleLifeActivity {
	public AdView getAdView(AdSize adSize);

	public static class AdmobImpl implements Admob {
		private String admobId;
		private InterstitialAd mInterstitialAd;
		private AdView mAdView;
		private AdRequest mAdRequest;

		public AdmobImpl(Activity activity) {
			admobId = MobileKitesApp.get().getKitesPrefers().getAdmobId();
			mInterstitialAd = new InterstitialAd(activity);
			mInterstitialAd.setAdUnitId(admobId);
			mAdRequest = new AdRequest.Builder().build();
			mInterstitialAd.loadAd(mAdRequest);
			mAdView = new AdView(activity);
			mAdView.setAdUnitId(admobId);
		}

		@Override
		public void onCreate() {

		}

		@Override
		public void onStart() {

		}

		@Override
		public void onResume() {
			mAdView.resume();
		}

		@Override
		public void onPause() {
			mAdView.pause();
		}

		@Override
		public void onStop() {

		}

		@Override
		public void onBackPressed() {
			if (mInterstitialAd.isLoaded())
				mInterstitialAd.show();
		}

		@Override
		public void onDestroy() {
			mAdView.destroy();
			mInterstitialAd = null;
			mAdRequest = null;
		}

		private boolean isSetSize = false;

		@Override
		public AdView getAdView(AdSize adSize) {
			if (!isSetSize)
				mAdView.setAdSize(adSize);
			isSetSize = true;
			mAdView.loadAd(mAdRequest);
			return mAdView;
		}

	}
}
